﻿package 
{
	import flash.events.Event;
	import flash.display.MovieClip;
	import flash.events.KeyboardEvent;
	import flash.ui.Keyboard;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.utils.Timer;
	import flash.media.SoundTransform;
	import flash.media.SoundChannel;
	import flash.text.TextField;

	public class PlatformGame extends MovieClip
	{

		private var musik:bgMusic;
		private var baggrundsmusik:SoundTransform;
		private var lydkanal:SoundChannel;

		private var lvlHandler:LevelHandler;

		private var grav:Number = 0;
		private var gacc:Number = 0.5;
		private var started:Boolean = false;

		private var level:Sprite;
		private var ground:Ground;
		private var man:Man;
		private var wall:Sprite;
		private var timer:Timer;
		private var startText:StartText;
		private var goalText:GoalText;
		private var levelText:TextField;

		private var up:Boolean = false;
		private var down:Boolean = false;
		private var right:Boolean = false;
		private var left:Boolean = false;

		public function PlatformGame()
		{
			musik = new bgMusic();
			baggrundsmusik = new SoundTransform(0.5,0);
			init();
		}
		private function init()
		{

			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;

			lvlHandler = new LevelHandler();

			ground = new Ground();
			ground.width = stage.stageWidth;
			addChild(ground);
			ground.x = stage.stageWidth/2;
			ground.y = stage.stageHeight-ground.height/2;


			level = new Sprite();
			addChild(level);

			man = new Man(-6);
			//addChild(man);
			//man.gotoAndStop(1);
			/*man.x = stage.stageWidth/2;
			man.y = ground.y;*/

			startText = new StartText();
			goalText = new GoalText();
			levelText = new TextField();
			levelText.textColor = 0xFFFFFF;
			addChild(levelText);
			startText.x = goalText.x = stage.stageWidth/2;
			startText.y = goalText.y = stage.stageHeight/2;
			createLvl();

			addEventListener(Event.ENTER_FRAME, onEnterFrame);
			stage.addEventListener(KeyboardEvent.KEY_DOWN, myKeyDown);
			stage.addEventListener(KeyboardEvent.KEY_UP, myKeyUp);
		}
		public function onEnterFrame(event:Event)
		{
			if (started)
			{
				if (man.Hit.hitTestObject(ground))
				{
					man.falling = false;
					grav = 0;
					man.y = ground.y;
					if (!man.running)
					{
						if (man.currentFrame != 4)
						{
							man.gotoAndStop(1);
						}
					}
				} else
				{
					man.falling = true;
				}
				for (var i=0; i<level.numChildren; i++)
				{
					var w = level.getChildAt(i);

					//if (man.hitTestObject(w)) {
					if (man.Hit.hitTestObject(w.Left))
					{
						if (w is Goal)
						{
							addChild(goalText);
							started = false;
						}
						level.x += man.xspeed;
					}
					if (man.Hit.hitTestObject(w.Right))
					{
						if (w is Goal)
						{
							addChild(goalText);
							started = false;
						}
						level.x -= man.xspeed;
					}
					if (man.Hit.hitTestObject(w.Top))
					{
						if (w is Goal)
						{
							addChild(goalText);
							started = false;
						}
						man.falling = false;
						grav = 0;
						man.y = w.y;
						if (!man.running)
						{
							if (man.currentFrame != 4)
							{
								man.gotoAndStop(1);
							}
						}
					}
					if (man.Hit.hitTestObject(w.Bottom))
					{
						if (w is Goal)
						{
							addChild(goalText);
							started = false;
						}
						man.falling = true;
						man.y = w.y+w.height+man.height;
						grav = 0;
					}
				}
				if (man.jumping && man.running && !man.falling)
				{
					man.gotoAndStop(2);
					man.jumping = false;
				}
				if (!man.falling && up)
				{
					grav = man.jumpspeed;
					man.falling = true;
					man.gotoAndStop(3);
					man.jumping = true;
				}
				if (man.falling)
				{
					man.y += grav;
					grav += gacc;
				}
				if (left)
				{
					level.x += man.xspeed;
					man.scaleX = -1;
				}
				if (right)
				{
					level.x -= man.xspeed;
					man.scaleX = 1;
				}
			}
		}
		private function myKeyDown(event:KeyboardEvent):void
		{

			if (event.keyCode == Keyboard.UP && !down)
			{
				up = true;
			}
			if (event.keyCode == Keyboard.RIGHT && !left)
			{
				right = true;
				if (!man.running && !man.falling && started)
				{
					man.gotoAndStop(2);
					man.running = true;
				}
			}
			if (event.keyCode == Keyboard.LEFT && !right)
			{
				left = true;
				if (!man.running && !man.falling && started)
				{
					man.gotoAndStop(2);
					man.running = true;
				}
			}
			if (event.keyCode == Keyboard.DOWN && !up)
			{
				down = true;
			}
			
			if (event.keyCode == Keyboard.SPACE)
			{
				if (goalText.stage == null)
				{
					started = !started;

					if (started)
					{
						removeChild(startText);
					} else
					{
						addChild(startText);
					}
					if (!musik.playing)
					{
						lydkanal = musik.play(0,10000,baggrundsmusik);
						musik.playing = true;
					}
				} else
				{
					if (lvlHandler.currentLevel == 1)
					{
						lvlHandler.currentLevel = 2;
					} else
					{
						lvlHandler.currentLevel = 1;
					}
					createLvl();
				}
			}
			
			// Level skipping... Debugging!
			if (event.keyCode == Keyboard.NUMPAD_0) 
			{
				if (lvlHandler.currentLevel == 1)
				{
					lvlHandler.currentLevel = 2;
				} else
				{
					lvlHandler.currentLevel = 1;
				}
				createLvl();
			}

			
		}
		private function myKeyUp(event:KeyboardEvent):void
		{

			if (event.keyCode == Keyboard.UP)
			{
				up = false;
			}
			if (event.keyCode == Keyboard.RIGHT)
			{
				right = false;
			}
			if (event.keyCode == Keyboard.LEFT)
			{
				left = false;
			}
			if (event.keyCode == Keyboard.DOWN)
			{
				down = false;
			}
			if (man.running && !left && !right)
			{
				man.running = false;
				if (!man.falling)
				{
					man.gotoAndStop(1);
				}
			}
		}
		public function createLvl():void
		{
			for (var k=level.numChildren-1; k>=0; k--)
			{
				level.removeChildAt(k);
			}
			level.x = 0;
			addChild(startText);
			started = false;
			if (goalText.stage != null)
			{
				removeChild(goalText);
			}
			levelText.text = "Level: " + lvlHandler.currentLevel;
			var lvlArray:Array = lvlHandler.getCurrentLevel();
			for (var i:int = 0; i<lvlArray.length; i++)
			{
				for (var j:int = 0; j<lvlArray[i].length; j++)
				{
					var newWall:Wall = new Wall();
					if (lvlArray[i][j] == 1)
					{
						newWall.x = j*newWall.width;
						newWall.y = i*newWall.height;
						level.addChild(newWall);
					} else if (lvlArray[i][j] == "MAIN")
					{
						man.x = j*newWall.width+newWall.width/2;
						man.y = i*newWall.height+newWall.height;
						addChild(man);
						man.gotoAndStop(4);
					} else if (lvlArray[i][j] == "GOAL")
					{
						var goal:Goal = new Goal();
						goal.x = j*newWall.width;
						goal.y = i*newWall.height;
						level.addChild(goal);
					}
				}
			}
			var manMoveX:int = stage.stageWidth/2 - man.x;

			man.x += manMoveX;
			level.x += manMoveX;
			//ground.y = level.height;

		}
	}
}